package com.vast.ui.panel.button.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;


import com.vast.ui.component.socketinterface.listner.ISocketConnectionListener;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.state.Sequence;
import com.vast.ui.panel.button.Activator;

public class ButtonPanel {
	private Composite         parent;
	private ScrolledComposite scrollPanel;
	private Button   buttonArray[];
	private Sequence sequence;
	private Image on;
	private Image off;
	private boolean isConnectiong;
	private int   numberOfButton;

	public ButtonPanel(Composite parent, Sequence sequence, RequestQueue sendQueue, int numberOfButton) {
		this.parent   = parent;
		this.sequence = sequence;
		
		this.numberOfButton   = numberOfButton;
		on = Activator.getDefault().getImageRegistry().getDescriptor("ON").createImage();
		off = Activator.getDefault().getImageRegistry().getDescriptor("OFF").createImage();
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 8;
		layout.marginTop = 0;
		layout.marginBottom = 0;
		layout.marginLeft = 0;
		layout.marginRight = 0;
		GridData layoutData;
		layoutData = new GridData(GridData.VERTICAL_ALIGN_CENTER);
		

		ScrolledComposite scrolPanel = new ScrolledComposite (parent, SWT.V_SCROLL | SWT.H_SCROLL);
		Composite panel = new Composite(scrolPanel, SWT.NONE);
		panel.setLayout(layout);
		buttonArray = new Button[numberOfButton];
		for (int i = 0; i < numberOfButton; i++) {
			buttonArray[i] = new Button(panel, SWT.TOGGLE | SWT.FLAT);
			buttonArray[i].setImage(off);
			buttonArray[i].setText("No." + i);
			buttonArray[i].setToolTipText(Integer.toString(i));
			buttonArray[i].addSelectionListener(new ButtonSelectProvider(sequence, sendQueue, on, off));
			
			layoutData = new GridData(GridData.VERTICAL_ALIGN_CENTER);
			buttonArray[i].setLayoutData(layoutData);
		}
		scrolPanel.setContent(panel);
		//scrolPanel.setExpandHorizontal(true);
		//scrolPanel.setExpandVertical(true);

		panel.setSize(panel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	public void resetButtonArray() {
		for (int i = 0; i < numberOfButton; i++) {
			buttonArray[i].setSelection(false);
			Event ee = new Event();
			ee.widget = buttonArray[i];
			SelectionEvent se = new SelectionEvent(ee);
		}
	}
}
